package public

import (
	"errors"
	"regexp"
	"strings"
)

//校验手机号码
func CheckTelPhone(telPhoneNum string) error {
	reg := `^1([38][0-9]|14[579]|5[^4]|16[6]|7[1-35-8]|9[189])\d{8}$`
	rgx := regexp.MustCompile(reg)
	if !rgx.MatchString(telPhoneNum) {
		return errors.New("手机号码有误")
	}
	return nil
}

//校验邮箱
func CheckEmail(telPhoneNum string) error {
	//reg := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`
	//reg := "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
	reg := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`
	rgx := regexp.MustCompile(reg)
	if !rgx.MatchString(telPhoneNum) {
		return errors.New("邮箱有误")
	}
	return nil
}

//过滤编码为四位的字符
func Utf8mb4Filtration(utf8mb4 string) string {
	arr := strings.Split(utf8mb4, "")
	strNew := ""
	for _, v := range arr {
		if len([]byte(v)) <= 3 {
			strNew = strNew + v
		}
	}
	return strNew
}

//
func Utf8mb4FiltrationStar(utf8mb4 string) string {
	arr := strings.Split(utf8mb4, "")
	strNew := ""
	for _, v := range arr {
		if len([]byte(v)) <= 3 {
			strNew = strNew + v
		} else {
			strNew = strNew + "*"
		}
	}
	return strNew
}
